var app = getApp();
var time = require("../../util");
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    TabCur: 0,
    scrollLeft: 0,
    tabName: ['进行中', '已完成'],
    list: {},
    isShowConfirm: false,
    adminPass: ''
  },
  onLoad() {
    var that=this
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              that.setData({ canIUse: true })
            }
          })
        }
      }
    })
  }, refreshList() {
    var that = this
    wx.cloud.callFunction({ name: 'function', data: { type: "gettask" } }).then(res => {
      var tmpData = []
      res.result.data.forEach(element => {
        if (element.type == this.data.type) {
          var tmpEle = element
          tmpEle.upTime = time.formatTimeTwo(tmpEle.upTime, "Y年M月D日h:m")
          tmpData.push(tmpEle)
        }
      });
      that.setData({ list: tmpData })
    })
  }, bindGetUserInfo(e) {
    this.setData({ hasLogin: true, type: "进行中" })
    this.refreshList()
    app.globalData.hasLogin=true
  }, select(e) {
    this.setData({ TabCur: e.currentTarget.dataset.id, scrollLeft: (e.currentTarget.dataset.id - 1) * 60 })
    if (e.currentTarget.dataset.id == 0) {
      this.setData({ type: "进行中" })
      this.refreshList()
    } else {
      this.setData({ type: "已完成" })
      this.refreshList()
    }
  }, listTap(a) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          var itemData = that.data.list[a.currentTarget.dataset.id]
          wx.cloud.callFunction({ name: 'function', data: { type: "deleteTask", fileId: itemData.fileId, id: itemData._id } }).then(res=>{
            that.refreshList()
          })
        }
      }
    })
  },watchPass(a) {
    this.setData({
      adminPass: a.detail.value
    })
  },cancel: function () {
    var that = this
    that.setData({
      isShowConfirm: false,
    })
  },confirmAcceptance: function () {
    this.setData({ isShowConfirm: false, })
    if (this.data.adminPass == "815815") {
      wx.navigateTo({url: '../admin/admin'})
    }
  }, admin() {
    this.setData({ isShowConfirm: true })
  },onShow(){
    this.refreshList()
  },onShareAppMessage(){

  },onShareTimeline(){}
})